var ctx;
var goodsId;

function setCtx(ctx){
	this.ctx = ctx;
}

function SignIn() { 
	var userId = $("#currentUserId").val();
	$.post(ctx +"/orderInfo/signIn", {
		userId:userId
	}, function(data) {
		data = JSON.parse(data);
		if (data.success) {
			layer.msg(data.msg, {
				icon : 1,
				time : 2000// 2秒关闭（如果不配置，默认是3秒）
			}, function() {
				window.location.href= ctx +"/main?item=6" ;
			});
		} else {
			layer.msg(data.msg, {
				icon : 2,
				time : 2000
			// 2秒关闭（如果不配置，默认是3秒）
			});
		}
	});
}

function ifNull(mess){
	if(mess == null || mess == undefined){
		mess = "";
	}
	return mess;
}

function gotoPage(page) {
	var pages = $("#pages").val();
	var states = $("#states").val();
	if(page == null || page == 'undefined'){
		page = this.page;
	}
	if(page < 1 ){
		layer.msg("已到达第一页!", {
			time : 1000
		// 2秒关闭（如果不配置，默认是3秒）
		});
	} else if(page > pages){
		layer.msg("已到达最后一页!", {
			time : 1000
		// 2秒关闭（如果不配置，默认是3秒）
		});
	} else if(states != null && states != ""){
		
			window.location.href= ctx +"/orderInfo/orderInfoListByState?pNo="+ page + "&states=" + states;	
		} else {
			window.location.href= ctx +"/orderInfo/orderInfoListByState?pNo="+ page ;
		}
	
}

//function gotoPage(page){
//	var states = $("#states").val();
//	if(states != null && states != ""){
//		window.location.href= ctx +"/orderInfo/orderInfoListByState?pNo="+ page + "&states=" + states;	
//	} else {
//		window.location.href= ctx +"/orderInfo/orderInfoListByState?pNo="+ page ;
//	}
//	
//}
//
//function gotoorderInfosPage(page) {
//	if(page == null){
//		page = $(".inputclass").val();
//	}
//	var states = $("#states").val();
//	if(states != null && states != ""){
//		window.location.href= ctx +"/orderInfo/orderInfoListByState?pNo="+ page + "&states=" + states;	
//	} else {
//		window.location.href= ctx +"/orderInfo/orderInfoListByState?pNo="+ page ;
//	}
//}

function checkReason(){
	var reason = $("#reason").val();
	if(reason == null || reason == ""){
		layer.tips('请填写问题描述!', '#reason', {
			tips: 1,
			time:1000
		});
		return false;
	}  else{
		return true;
	}
}

function checkCommentContent(){
	var commentContent = $("#commentContent").val();
	if(commentContent == null || commentContent == ""){
		layer.tips('请填写问题描述!', '#commentContent', {
			tips: 1,
			time:1000
		});
		return false;
	}  else{
		return true;
	}
}

//评价
function comment(goodId, orderInfoId){
	var goodsId = ifNull(goodId);
	var orderId = ifNull(orderInfoId);
	window.location.href= ctx + "/orderInfo/comment?goodsId=" + goodsId + "&orderId=" + orderId;
}

//保存评价
function saveComment(){
	var goodsId = ifNull($("#goodsId").val());
	var content = ifNull($("#commentContent").val());
	var score = ifNull($("#level").val());
	var orderInfoId = ifNull($("#orderInfoId").val());
	var specificationId = ifNull($("#specificationId").val());
	
	
	if(checkCommentContent()){
		var index = layer.load(1);
		$.post(ctx +"/orderInfo/saveComment", {
			goodsId:goodsId,
			content:content,
			score:score,
			specificationId:specificationId
		}, function(data) {
			data = JSON.parse(data);
			if (data.success) {
				layer.msg("操作成功", {
					icon : 1,
					time : 1000// 2秒关闭（如果不配置，默认是3秒）
				}, function() {
					layer.close(index);
					window.location.href= ctx +"/orderInfo/orderDetail?orderInfoId=" + orderInfoId;
				});
			} else {
				layer.msg(data.msg, {
					icon : 2,
					time : 2000
				// 2秒关闭（如果不配置，默认是3秒）
				});
				layer.close(index);
			}
		});
	}
}

//订单详情
function orderDetail(orderId){
	var orderInfoId = ifNull(orderId);
	window.location.href= ctx + "/orderInfo/orderDetail?orderInfoId="+orderInfoId;
	
}

//从评价页面返回上一页面即详情页面
function backOrderDetail(orderId){
	var orderInfoId = ifNull(orderId);
	window.location.href= ctx + "/orderInfo/orderDetail?orderInfoId="+orderInfoId;
}

//从详情页面返回上一页面即订单页面
function backMyOrder(){
	gotoPage(1);
}

//从详情页面返回上一页面即订单页面
function complainSeller(){
	var shopId = ifNull($("#shopId").val());
	window.location.href= ctx + "/orderInfo/complainSeller?shopId="+shopId ;
}

//申请退款，申请退货
function applyRefundOrGoods(orderInfoId){
	window.location.href= ctx + "/orderInfo/applyRefundOrGoods?orderInfoId="+orderInfoId;
}

//提交退款退货申请
function saveRefundAfter(orderInfoId,state){
	var reason = $("#reason").val();
	var applyReason = ifNull(reason);
	
	if(checkReason()){
		var index = layer.load(1);
		$.post(ctx +"/orderInfo/saveApply", {
			orderInfoId:orderInfoId,
			state:state,
			applyReason:applyReason
		}, function(data) {
			data = JSON.parse(data);
			if (data.success) {
				layer.msg("操作成功", {
					icon : 1,
					time : 1000// 2秒关闭（如果不配置，默认是3秒）
				},function() {
					layer.close(index);
					window.location.href= ctx +"/orderInfo/orderInfoListByState?pNo=1";
				});
			} else {
				layer.msg(data.msg, {
					icon : 2,
					time : 2000
				// 2秒关闭（如果不配置，默认是3秒）
				});
				layer.close(index);
			}
		});
	}
	
}

//取消订单
function cancelOrder(orderInfoId){
	layer.confirm('确认取消该订单?', {icon: 3, title:'提示',skin:'exit',area:['320px','200px']}, function(index) {
		$.post(ctx + "/orderInfo/cancelOrder", {
			orderInfoId : orderInfoId
		}, function(data) {
			data = JSON.parse(data);
			if (data.success) {
				layer.msg("订单已取消", {
					icon : 1,
					time : 2000
				// 2秒关闭（如果不配置，默认是3秒）
				}, function() {
					window.location.href= ctx +"/orderInfo/orderInfoListByState?pNo=1";
				});
			} else {
				layer.msg(data.msg, {
					icon : 2,
					time : 2000
				// 2秒关闭（如果不配置，默认是3秒）
				});
			}
		});
		layer.close(index);
	})
}

//确认收货
function confirmReceiveGoods(orderInfoId){
	layer.confirm('确认收货?', {icon: 3, title:'提示',skin:'exit',area:['320px','200px']}, function(index) {
		$.post(ctx + "/orderInfo/confirmReceiveGoods", {
			orderInfoId : orderInfoId
		}, function(data) {
			data = JSON.parse(data);
			if (data.success) {
				layer.msg("收货已确认", {
					icon : 1,
					time : 2000
				// 2秒关闭（如果不配置，默认是3秒）
				}, function() {
					window.location.href= ctx +"/orderInfo/orderInfoListByState?states=3,14";
				});
			} else {
				layer.msg(data.msg, {
					icon : 2,
					time : 2000
				// 2秒关闭（如果不配置，默认是3秒）
				});
			}
		});
		layer.close(index);
	})
}

//确认消费
function confirmConsume(orderInfoId){
	layer.confirm('确认消费?', {icon: 3, title:'提示',skin:'exit',area:['320px','200px']}, function(index) {
		$.post(ctx + "/orderInfo/confirmReceiveGoods", {
			orderInfoId : orderInfoId
		}, function(data) {
			data = JSON.parse(data);
			if (data.success) {
				layer.msg("已确认消费", {
					icon : 1,
					time : 2000
				// 2秒关闭（如果不配置，默认是3秒）
				}, function() {
					window.location.href= ctx +"/orderInfo/orderInfoListByState?states=3,14";
				});
			} else {
				layer.msg(data.msg, {
					icon : 2,
					time : 2000
				// 2秒关闭（如果不配置，默认是3秒）
				});
			}
		});
		layer.close(index);
	})
}

//立即支付
function payMoney (orderInfoId){
	layer.open({
		type: 2,
		title: '订单支付',
		content: ctx + "/orderInfo/payMoney?orderInfoId="+orderInfoId,
		skin: "house-add",
		area: ["705px","480px"],
		end: function(){
		}
	});
}

// 五星打分
var Class = {
		create : function() {
			return function() {
				this.initialize.apply(this, arguments);
			}
		}
}
var Extend = function(destination, source) {
	for ( var property in source) {
		destination[property] = source[property];
	}
}
function stopDefault(e) {
	if (e && e.preventDefault) {
		e.preventDefault();
	} else {
		window.event.returnValue = false;
	}
	return false;
}
/**
 * 星星打分组件
 */
var Stars = Class.create();
var score;
Stars.prototype = {
		initialize : function(star, options) {
			this.SetOptions(options); // 默认属性
			var flag = 999; // 定义全局指针
			var isIE = (document.all) ? true : false; // IE?
			if(undefined == document.getElementById('stars1')) {
				return;
			}
			var starlist = document.getElementById(star).getElementsByTagName(
			'a'); // 星星列表
			var input = document.getElementById(this.options.Input)
			|| document.getElementById(star + "-input"); // 输出结果
			
			var tips = document.getElementById(this.options.Tips)
			|| document.getElementById(star + "-tips"); // 打印提示
			var nowClass = " " + this.options.nowClass; // 定义选中星星样式名
			var tipsTxt = this.options.tipsTxt; // 定义提示文案
			var len = starlist.length; // 星星数量
			var nowTip = '';
			for (i = 0; i < len; i++) { // 绑定事件 点击 鼠标滑过
				starlist[i].value = i;
				starlist[i].onclick = function(e) {
					stopDefault(e);
					this.className = this.className + nowClass;
					flag = this.value;
					input.value = this.getAttribute("star:value");
					tips.innerHTML = tipsTxt[this.value];
					$("#level").val(input.value);
					nowTip = this.value;
				}
				starlist[i].onmouseover = function() {
					if (flag < 999) {
						var reg = RegExp(nowClass, "g");
						tips.innerHTML = tipsTxt[this.value];
						starlist[flag].className = starlist[flag].className
						.replace(reg, "");
						// tips.innerHTML = tipsTxt[this.value]
					}
				}
				starlist[i].onmouseout = function() {
					if (flag < 999) {
						starlist[flag].className = starlist[flag].className
						+ nowClass;
						tips.innerHTML = tipsTxt[nowTip];
					}
				}
			}
			;
			if (isIE) { // FIX IE下样式错误
				var li = document.getElementById(star).getElementsByTagName(
				'li');
				for (var i = 0, len = li.length; i < len; i++) {
					var c = li[i];
					if (c) {
						c.className = c.getElementsByTagName('a')[0].className;
					}
				}
			}
		},
		// 设置默认属性
		SetOptions : function(options) {
			this.options = {// 默认值
					Input : "",// 设置触保存分数的INPUT
					Tips : "",// 设置提示文案容器
					nowClass : "current-rating",// 选中的样式名
					tipsTxt : [ "1星 - 很差", "2星 - 不怎么好", "3星- 一般吧", "4星 - 不错",
					            "5星 - 非常好" ]
			// 提示文案
			};
			Extend(this.options, options || {});
		}
}
$(function (){
	var Stars1 = new Stars("stars1", {
		nowClass : "current-rating",
		tipsTxt : [ "1星 - 很差", "2星 - 不怎么好", "3星- 一般吧", "4星 - 不错", "5星 - 非常好" ]
	})

});

function aliPay(info){
	$('#returnAli').append(info);
}

function wxPay(){
	window.location.href = ctx + "/orderInfo/orderInfoListByState?states=0"
}